package com.atguigu.sparkcore.day03.action

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * Author atguigu
 * Date 2020/10/30 9:49
 */
object AggregateDemo {
    def main(args: Array[String]): Unit = {
        val conf: SparkConf = new SparkConf().setAppName("AggregateDemo").setMaster("local[2]")
        val sc: SparkContext = new SparkContext(conf)
        val list1 = List(30, 50, 70, 60, 10, 20)
        val rdd1: RDD[Int] = sc.parallelize(list1, 3)
        //val r = rdd1.reduce(_ + _)
        // 零值参与运算的次数: 分区数+1
        // val r = rdd1.fold(1)(_ + _)
        //  val r = rdd1.fold(Int.MinValue)((x, y) => x.max(y))
        val r = rdd1.aggregate(Int.MinValue)(math.max, _ - _) - Int.MinValue
        println(r)
        sc.stop()
        
    }
}

/*
用在任意的算子上做聚合:
    reduce
    fold
    aggregate
 */